﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace _52_鉴权授权.Controllers
{
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> UserLogin(string user, string pwd)
        {
            if ("111".Equals(user) && "222".Equals(pwd))
            {
                var claims = new List<Claim>() // 鉴别你是谁的相关信息
                {
                    new Claim("Userid","1"),
                    new Claim(ClaimTypes.Role,"Admin"),
                    new Claim(ClaimTypes.Role,"User"),
                    new Claim(ClaimTypes.Name,$"{user}--来源于Cookies"),
                    new Claim(ClaimTypes.Email,"1111@qq.com"),
                    new Claim("password",pwd),
                    new Claim("Account","Administrator"),
                    new Claim("role","admin")

                };
                ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "Customer"));
                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties
                {
                    ExpiresUtc = DateTime.UtcNow.AddMinutes(30) // 30分钟过期
                }).Wait();
                var user1 = HttpContext.User;
                return base.Redirect("/Home/Index");
            }
            else {
                // base.ViewBag.Msg = "用户账号密码错误";
                // return null;
                return Json(new
                {
                    id = "11",
                    name = "222"
                }); ;
            }
        }
    }
}
